package com.example.demo.controller;

import com.example.demo.config.ResponseModel;
import com.example.demo.entity.Cart;

import com.example.demo.exception.BusinessException;
import com.example.demo.service.CartService;
import com.example.demo.util.KafkaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chenqiuran
 * @decription
 * @date 2021/8/31
 */
@Api(value = "购物车controller",tags = "购物车操作接口")
@RestController
@RequestMapping("/cart")
public class CartController extends BaseController{
    @Autowired
    private CartService cartService;


    //添加商品到购物车
    public ResponseModel addCart(Cart cart){
        cartService.addCart(cart);
        return new ResponseModel<>(null, "success", "200");
    }

    //查看购物车所有菜品
    public ResponseModel<List<Cart>> findAll(Long userId){
        List<Cart> cartList = cartService.findAll(userId);
        return new ResponseModel<>(cartList, "success", "200");
    }

    //增加菜品数量
    public ResponseModel productAmountASC(Long userId, Long menuId,Integer amount){
        cartService.productAmountASC(userId,menuId,amount);
        return new ResponseModel<>(null,"success","200");
    }

    //减少菜品数量
    public ResponseModel productAmountDESC(Long userId, Long menuId){
        cartService.productAmountDESC(userId,menuId);
        return new ResponseModel<>(null,"success","200");
    }

    public ResponseModel<Cart> findById(Long userId, Long menuId){
        Cart cart = cartService.findById(userId,menuId);
        return new ResponseModel<Cart>(cart,"success","200");
    }

    //删除菜品
    public ResponseModel delCart(Long userId, Long menuId) throws BusinessException {
        cartService.delCart(userId,menuId);
        return new ResponseModel(null,"success","200");
    }


//    @Autowired
//    private CartService cartService;
//
//    @ApiOperation(value="将菜品添加到购物车",tags={"将菜品添加到购物车"},httpMethod = "POST")
//    @PostMapping(value = "/addCart")
//    public ResponseModel addCart(Cart cart) throws BusinessException {
//        Boolean flag = cartService.addCart(cart)>0;
//        if(flag){
//            return new ResponseModel<>(null,"success","200");
//        }else{
//            //状态码和HTTP状态码保持一致
//            return new ResponseModel<>(null,"failed","500");
//        }
//    }
//
//    @ApiOperation(value="查看购物车中所有菜品",tags={"查看购物车菜品"},httpMethod = "GET")
//    @GetMapping(value = "/findAll/{userId}")
//    @ResponseBody
//    public CommonReturnType findAll(@PathVariable Long userId) throws BusinessException {
//        List<Menu> menuList = cartService.findAll(userId);
//        return new CommonReturnType(0,"",100,menuList);
//    }
//
//    @PostMapping("/productAmountASC/{cartId}")
//    @ApiOperation(value="增加菜品数量",tags={"增加菜品数量"},httpMethod = "POST")
//    public ResponseModel productAmountASC(@PathVariable Long cartId){
//        Boolean flag = cartService.productAmountASC(cartId)>0;
//        if(flag){
//            return new ResponseModel<>(null,"success","200");
//        }else{
//            //状态码和HTTP状态码保持一致
//            return new ResponseModel<>(null,"failed","500");
//        }
//    }
//
//    @PostMapping("/productAmountDESC/{cartId}")
//    @ApiOperation(value="减少菜品数量",tags={"减少菜品数量"},httpMethod = "POST")
//    public ResponseModel productAmountDESC(@PathVariable Long cartId){
//        Boolean flag = cartService.productAmountDESC(cartId)>0;
//        if(flag){
//            return new ResponseModel<>(null,"success","200");
//        }else{
//            //状态码和HTTP状态码保持一致
//            return new ResponseModel<>(null,"failed","500");
//        }
//    }
//
//    @PostMapping("/delCart/{userId}")
//    @ApiOperation(value="清空购物车",tags={"清空购物车"},httpMethod = "POST")
//    public ResponseModel delCart(@PathVariable Long userId){
//        Boolean flag = cartService.delCart(userId)>0;
//        if(flag){
//            return new ResponseModel<>(null,"success","200");
//        }else{
//            //状态码和HTTP状态码保持一致
//            return new ResponseModel<>(null,"failed","500");
//        }
//    }
//
//    @GetMapping("/findById/{cartId}")
//    @ApiOperation(value="根据Id查找购物菜品",tags={"查看购物车菜品"},httpMethod = "GET")
//    public CommonReturnType findById(@PathVariable Long cartId) throws BusinessException {
//        Cart cart = cartService.findById(cartId);
//        return new CommonReturnType(0,"",100,cart);
//    }
}
